今天和大家介紹第二種寫法:自行編輯Flex Message語法,客製化聊天機器人回覆訊息。
首先先詳細的介紹彈性配置的基本架構
為建立一個矩形區塊,區塊可放置其他彈性配置元件,也是使用最多的彈性配置元件。
常用屬性
練習程式
BoxComponent(
layout = 'vertical',
spacing='md',
margin='sm',
contents = [
TextComponent(...), #元件一
IconComponent(...), #元件二
...
]
),
為顯示文字的彈性配置元件
常用屬性
練習程式
TextComponent(
text = '國立臺北教育大學',
weight='bold',
size='lg'
flex=2,
align='center',
color='##0070C0'
)
為顯示圖片的彈性配置元件
常用屬性
練習程式
ImageComponent(
url='https://i.imgur.com/3sBRh08.jpg',
size = 'full', #圖片最大尺寸為full
aspect_ratio = '700:700',
aspect_mode = 'cover',
),
為建立按鈕的彈性配置元件
常用屬性
練習程式
ButtonComponent(
action = URIAction(label='查看網頁',uri='https://www.ntue.edu.tw/'),
style = 'primary',
height= 'sm',
color='##0070C0'
)
為顯示Icon圖示的彈性配置元件
為顯示水平線的彈性配置元件
需於func.py最上方匯入模組
func.py程式碼
def sendFlex(event):
try:
bubble = BubbleContainer(
direction = 'ltr',
header = BoxComponent( #標題
layout = 'vertical',
contents = [
TextComponent(text = '國立臺北教育大學', weight='bold', size='lg'),
]
),
hero = ImageComponent( #主圖片
url='https://i.imgur.com/UzSXGRX.jpg',
size = 'full',
aspect_ratio = '700:700',
aspect_mode = 'cover',
),
body = BoxComponent( #主要內容
layout = 'vertical',
contents = [
TextComponent(text='評價', size='md'),
BoxComponent(
layout = 'baseline',
margin='md',
contents=[
IconComponent(size='lg', url = 'https://i.imgur.com/GsWCrIx.png'),
TextComponent(text='25', size='sm', color='#999999', flex=0),
IconComponent(size='lg', url = 'https://i.imgur.com/sJPhtB3.png'),
TextComponent(text='14', size='sm', color='#999999', flex=0),
]
),
BoxComponent(
layout = 'vertical',
margin='lg',
contents=[
BoxComponent(
layout = 'baseline',
contents=[
TextComponent(
text='營業地址',
size='sm',
color='#aaaaaa',
flex=2
),
TextComponent(
text='臺北市大安區和平東路2段134號',
size='sm',
color='#666666',
flex=5
),
]
),
SeparatorComponent(color='#0000FF'),
BoxComponent(
layout = 'baseline',
contents=[
TextComponent(
text='營業時間',
size='sm',
color='#aaaaaa',
flex=2
),
TextComponent(
text='10:00 - 23:00',
size='sm',
color='#666666',
flex=5
),
]
),
]
),
BoxComponent(
layout = 'horizontal',
margin='xxl',
contents=[
ButtonComponent(
style = 'primary',
height= 'sm',
action = URIAction(
label='電話聯絡',
uri='tel:0227321104'
),
),
ButtonComponent(
style = 'secondary',
height= 'sm',
action = URIAction(
label='查看網頁',
uri='https://www.ntue.edu.tw/'
),
)
]
)
],
),
footer = BoxComponent(
layout = 'vertical',
contents = [
TextComponent(
text='Copyright @ntue',
color = '#888888',
size='sm',
align='center'
),
]
),
)
message = FlexSendMessage(alt_text="彈性配置範例", contents=bubble)
line_bot_api.reply_message(event.reply_token,message)
except:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text='發生錯誤!'))
第一步驟可建立圖文選單以方便樣板訊息的測試,圖文選單可參考DAY5的貼人賽文章哦,而下方測試畫面的網頁連結就先省略範例示圖~
接下來和大家介紹DAY11 Flex message(上篇)中有提到的LIFF吧( •̀ ω •́ )✧